﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_AdminProductAppleTV : System.Web.UI.Page
{
    private string connectionString;
    private SqlConnection connect;
    private SqlDataAdapter da;
    private SqlCommand command;
    private DataSet ds;
    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        connect = new SqlConnection(connectionString);
        command = new SqlCommand();
        if (!IsPostBack)
        {
            bindDataToGridView();
        }

    }

    protected void bindDataToGridView()
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetAllProductAppleTVDetails", connect);
            command.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            connect.Close();
        }
    }

    protected void getDetails(int ID)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spGetProductAppleTVDetailsByNo", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@No", SqlDbType.Int).Value = ID;
            command.ExecuteNonQuery();
            da = new SqlDataAdapter(command);
            da.Fill(ds);
            //string img1 = ds.Tables[0].Rows[0][4].ToString();
            //string name1 = ds.Tables[0].Rows[0][1].ToString();
            //string type1 = ds.Tables[0].Rows[0][2].ToString();
            //string price1 = ds.Tables[0].Rows[0][3].ToString();
            //string id1 = ds.Tables[0].Rows[0][0].ToString();
            int No = Int32.Parse(ds.Tables[0].Rows[0][0].ToString());
            string id = ds.Tables[0].Rows[0][1].ToString();
            string img = ds.Tables[0].Rows[0][2].ToString();
            string txt = ds.Tables[0].Rows[0][3].ToString();
            Response.Redirect("AdminProductAppleTVManager.aspx?Img="+img+"&No="+No+"&ID="+id+"&Txt="+txt+"&Status=Update");
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }

    protected void deleteDetails(int ID)
    {
        try
        {
            if (connect.State == ConnectionState.Closed)
            {
                connect.Open();
            }
            ds = new DataSet();
            command = new SqlCommand("spDeleteAppleTV", connect);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@No", SqlDbType.Int).Value = ID;
            command.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            connect.Close();
        }
    }

    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        //lblStatus.Text = "Ban chon " + row.Cells[1].Text + ".";
        getDetails(Int32.Parse(row.Cells[0].Text));
        //Label1.Text = row.Cells[0].Text;
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int id = Int32.Parse(GridView1.DataKeys[e.RowIndex].Values["DetailsNo"].ToString());
        deleteDetails(id);
        bindDataToGridView();
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("AdminProductAppleTVManager.aspx");
    }
}